<?php

class models_UserMapper extends models_MapperBase
{

//----------------------------------------------------------------------------------------------------
    /**
     * Inits user object
     *
     * @param $oRow
     * @return models_User
     */
	protected static function _initUser($oRow)
	{
		if (null === $oRow)
		{
			return null;
		}

		$oUser = new models_User();


		$oUser->id 					= $oRow->id;
		$oUser->email 				= $oRow->email;
		$oUser->password 			= $oRow->password;
		$oUser->role 				= $oRow->role;
		$oUser->name                = $oRow->name;

		return $oUser;
	}

//----------------------------------------------------------------------------------------------------

	private static $_dbTable = 'models_DbTable_User';

//----------------------------------------------------------------------------------------------------

	/**
	 * Saves or updates user
	 *
	 * @param models_User $oUser
	 * @return int
	 */
    public static function save(models_User $oUser)
    {
		return parent::saveToDb($oUser, self::$_dbTable);
    }
    
//----------------------------------------------------------------------------------------------------

    public static function saveArray($data)
    {
        $DbTable = self::_getDbTable(self::$_dbTable);
        $result = $DbTable->insert($data);
        return $result;
    }
    
//----------------------------------------------------------------------------------------------------
	
	/**
	 * @author Nikita
	 *
	 * @param $sField
	 * @param $mValue
	 * @return unknown_type
	 */
	public static function fetchByField($sField, $mValue)
	{
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$oSelect = $oDbTable->select();
		$oSelect->where('`'.$sField.'` = "'.$mValue.'"');
		$oResult = $oDbTable->fetchAll($oSelect);
		return self::_createArrayFromResultSet($oResult, array(__CLASS__, '_initUser'));
	}

    /**
     * Updates field in table
     *
     * @param int $iId
     * @param string $sFieldNamem
     * @param string $sFieldValue
     */
    public static function updateUser($iId, $sFieldName, $sFieldValue)
    {
		$aData = array(
			$sFieldName => $sFieldValue,
		);
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$where = $oDbTable->getAdapter()->quoteInto('id = ?', $iId, Zend_Db::INT_TYPE);
		$oDbTable->update($aData, $where);
	}

//----------------------------------------------------------------------------------------------------

    /**
     * Updates field in table
     *
     * @param int $iId
     * @param string $sFieldNamem
     * @param string $sFieldValue
     */
    public static function updateKey($key, $sFieldName, $sFieldValue)
    {
		$aData = array(
			'status' => 'active',
		);
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$where = $oDbTable->getAdapter()->quoteInto('user.key = ?', $key);
		return $oDbTable->update($aData, $where);
    }

//----------------------------------------------------------------------------------------------------

    public static function updateForgotKey($key, $sFieldName, $sFieldValue)
    {
		$aData = array(
			$sFieldName => $sFieldValue,
		);
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$where = $oDbTable->getAdapter()->quoteInto('user.forgot_key = ?', $key);
		return $oDbTable->update($aData, $where);
    }

//----------------------------------------------------------------------------------------------------


	/**
	 * Deletes user from table
	 *
	 * @param $iId
	 */
	public static function delete($iId)
	{
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$where = $oDbTable->getAdapter()->quoteInto('id = ?', $iId, Zend_Db::INT_TYPE);
		$oDbTable->delete($where);
	}

//----------------------------------------------------------------------------------------------------

    /**
     * Find user by id
     *
     * @param int $iId
     * @return models_User|null
     */
    public static function findById($iId)
    {
		$oDbTable = self::_getDbTable(self::$_dbTable);

		$oRegistry = Zend_Registry::getInstance();
		$oConfig = $oRegistry->get('config')->production->resources->db;
		$db = Zend_Db::factory($oConfig);
		$db->setFetchMode(Zend_Db::FETCH_OBJ);

		$oSelect = $oDbTable->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
		$oSelect ->where('id = ?', $iId);

//		$result = $db->fetchAssoc($oSelect->__tostring());
		$result = $db->fetchAll($oSelect);
		
		if (isset($result[0]))
		{
@		  list ($fn, $sn) = explode(' ', $result[0]->name);
		  $result[0]->afk = strtoupper(substr($fn, 0, 1) . substr($sn, 0, 2));
		
//	    print_r($result[0]); die;

		  return $result[0];
		}
		else
		{
			return array();
		}
    }

//----------------------------------------------------------------------------------------------------

    /**
     * Get the info of user in array
     *
     * @param int $iId
     * @return array|null
     */
    public static function fetchById($iId)
    {
        $oResult = self::_getDbTable(self::$_dbTable)->find($iId);
        return self::_initUser($oResult->current());
        // return self::_createArrayFromResultSet($oResult, array(__CLASS__, '_initUser'));
    }

//----------------------------------------------------------------------------------------------------

    public static function fetchAll()
    {
        $oResultSet = self::_getDbTable(self::$_dbTable)->fetchAll();
        return self::_createArrayFromResultSet($oResultSet, array(__CLASS__, '_initUser'));
    }

//----------------------------------------------------------------------------------------------------

    /**
     * Find user by email
     *
     * @param string $sEmail
     * @return models_User|null
     */
	public static function findByEmail($sEmail)
    {
        $oDbTable = self::_getDbTable(self::$_dbTable);
    	$oSelect = $oDbTable->select();
		$oSelect->where('email = ?', $sEmail);
        $oRow = self::_getDbTable(self::$_dbTable)->fetchRow($oSelect);
		return self::_initUser($oRow);
    }

//----------------------------------------------------------------------------------------------------

    public static function findByForgotKey($key)
    {
        $oDbTable = self::_getDbTable(self::$_dbTable);
    	$oSelect = $oDbTable->select();
		$oSelect->where('forgot_key = ?', $key);
        $oRow = self::_getDbTable(self::$_dbTable)->fetchRow($oSelect);
		return self::_initUser($oRow);
    }

//----------------------------------------------------------------------------------------------------

    public static function setPaid($sEmail)
    {
		$aData = array(
			'account_type' => 'full',
		);
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$where = $oDbTable->getAdapter()->quoteInto('email = ?', $sEmail);
		$oDbTable->update($aData, $where);
    }

//----------------------------------------------------------------------------------------------------

    public static function updateSessionId($iUserId, $sSessionId)
    {
		$aData = array(
			'session_id' => $sSessionId,
		);
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$where = $oDbTable->getAdapter()->quoteInto('id = ?', $iUserId, Zend_Db::INT_TYPE);
		$oDbTable->update($aData, $where);
    }

//--------------------------------------------------------------------------------------------------
	/**
	* Updates field in table
	*
	* @param int $iId
	* @param string $sFieldNamem
	* @param string $sFieldValue
	*/
	public static function updateArray($iId, $aData)
	{
		$oDbTable = self::_getDbTable(self::$_dbTable);
		$where = $oDbTable->getAdapter()->quoteInto('id = ?', $iId, Zend_Db::INT_TYPE);
		$ret = $oDbTable->update($aData, $where);
		return $ret;
	}

//----------------------------------------------------------------------------------------------------

	public static function getRole($iId)
	{
		$DbTable = self::_getDbTable(self::$_dbTable);
		$Select = $DbTable->select();
		$Select->setIntegrityCheck(false);
		$db = self::_initDb();

		$Select ->where('id = ?', $iId);

		$result = $db->fetchAll($Select);
		return $result[0]['role'];
	}


}	// CLASS